import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_he/utils/xin_he_toast.dart';

import '../../../../routes/xin_he_routes.dart';
import '../../../../utils/xh_styles.dart';
import '../../../../utils/xin_he_image.dart';
import '../../../goods/xin_he_good_detail/xin_he_goods_specification_model_entity.dart';
import 'xin_he_instant_buy_logic.dart';
///商品详情->>立即购买
class XinHeInstantBuyPage extends StatelessWidget {
  const XinHeInstantBuyPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(XinHeInstantBuyLogic());
    final state = Get.find<XinHeInstantBuyLogic>().state;
    return Obx(() =>  Container(
      height: 600+Get.mediaQuery.padding.bottom,
      width: Get.width,
      color: Colors.white,
      child: Stack(children: [
        Column(

          children: [
            ///关闭
            Row(children: [
              const Text('  '),
              Spacer(),
              GestureDetector(
                onTap: (){
                  Get.back();
                },
                child: Container(
                    padding: const EdgeInsets.all(8),
                    margin: const EdgeInsets.only(right: 7),
                    child: const XinHeAssetImage('assets/images/mine/sheet_close.png',width: 15,height: 15,)),
              ),
            ],),
            Obx(() => _itemView()),


            ///选择地址
            GestureDetector(
              onTap: (){
                Get.toNamed(XinHeRoutes.addressManagement);

              },
              child: Container(
                color: Colors.transparent,
                margin: const EdgeInsets.symmetric(horizontal: 15,vertical: 15),
                child: Row(children: [

                  const XinHeAssetImage('assets/images/cart/pay_location.png',width: 15,height: 19,),
                  const SizedBox(width: 5,),
                  Text(logic.addressStr.value,style: TextStyle(color: XinHeColor('#333333'),fontSize: 14,fontWeight: FontWeight.w500), maxLines: 1,),
                  const Spacer(),
                  const XinHeAssetImage('assets/images/cart/pay_arrow.png',width: 6,height: 10,),
                ],),
              ),
            ),

            ///数量
            Padding(

              padding: const EdgeInsets.symmetric(horizontal: 15,vertical: 10),
              child: Row(
                // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text('数量',style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold)),
                  Spacer(),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      GestureDetector(
                        onTap: (){

                          // if(currentSpecM.value.isSelected == false) {
                          //   XinHeToast.center('请先选择规格');
                          // }
                          logic.handleItemCount(logic.specM.value.price??0, false);
                        },
                        child: Container(
                          height: 33,
                          width: 33,
                          alignment: Alignment.center,
                          margin: const EdgeInsets.only(left: 10),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(16.5),
                              border: Border.all(color: XinHeColor('#E9EFF0'),width: 0.5)
                          ),
                          child: Text('-',style: TextStyle(color:XinHeColor('#8B9191'),fontSize: 16,fontWeight: FontWeight.w600),
                          ),
                        ),
                      ),

                      Container(
                        height: 33,
                        margin: const EdgeInsets.only(left: 10),
                        padding: const EdgeInsets.symmetric(horizontal: 6),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: XinHeColor('#E9EFF0')

                        ),
                        child: Text('${logic.count.value}',style: TextStyle(color:XinHeColor('#8B9191'),fontSize: 16,fontWeight: FontWeight.w600),
                        ),
                      ),

                      GestureDetector(
                        onTap: (){
                          if(logic.specM.value.isSelected == false) {
                            // XinHeToast.center('请先选择规格');
                            // return;
                          }
                          logic.handleItemCount(logic.specM.value.price??0, true);
                        },
                        child: Container(
                          height: 33,
                          width: 33,
                          margin: const EdgeInsets.only(left: 10),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(16.5),
                              border: Border.all(color: XinHeColor('#E9EFF0'),width: 0.5)
                          ),
                          child: Text('+',style: TextStyle(color:XinHeColor('#8B9191'),fontSize: 16,fontWeight: FontWeight.w600),
                          ),
                        ),
                      ),


                    ],),
                  const SizedBox(width: 15,),
                ],),
            ),


            ///商品规格
            Container(
                width: double.infinity,
                margin: const EdgeInsets.symmetric(horizontal: 15,vertical: 5),
                child: const Text('规格',style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),)),
            Expanded(child: ListView.builder(
                itemCount: logic.specList.value.length,
                itemBuilder: (ctx, index)
                 {
                     XinHeGoodsSpecificationModelEntity model = logic.specList.value[index];
                     return GestureDetector(
                       onTap: (){


                         for(int i = 0; i< logic.specList.length;i++){
                           if(i != index) {
                             XinHeGoodsSpecificationModelEntity e = logic.specList[i];
                             e.isSelected = false;
                             logic.specList[i] = e;
                           }else{
                             model.isSelected = !model.isSelected;
                             logic.specList[index] = model;
                           }
                         }
                         // currentSpecM.value = model;
                         logic.specM.value = model;
                         logic.reCalculateTotalAmount(logic.specM.value.price!);

                       },
                       child: Container(
                         height: 40,
                         alignment: Alignment.centerLeft,
                         margin: const EdgeInsets.symmetric(horizontal: 15,vertical: 5),
                         decoration: BoxDecoration(
                           borderRadius: BorderRadius.circular(5),
                           color: const Color.fromARGB(255, 247, 247, 247),
                           border: Border.all(color:  model.isSelected?XinHeColor('#E84128'):Colors.transparent, width: model.isSelected?1:0)

                         ),  
                           child: Text(model.name??''),),
                     );
                 })),



            SizedBox(height: Get.mediaQuery.padding.bottom + 45,)

          ],



        ),

        Positioned(
          bottom: Get.mediaQuery.padding.bottom,
          child: SizedBox(
            height: 45,
            width: Get.width,
            child: Row(children: [
              const Spacer(),
              RichText(text: TextSpan(
                  children: [

                    TextSpan(
                      text:'共计',
                      style: TextStyle(
                        color: XinHeColor('##2B3131'),
                        fontSize: 11,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    ///￥
                    TextSpan(
                      text: '￥',
                      style: TextStyle(
                        color: XinHeColor('#E84128'),
                        fontSize: 9,
                        fontWeight: FontWeight.w500,
                      ),
                    ),

                    ///
                    TextSpan(
                      text: logic.specM.value.isSelected == false? '0.00' : state.totalPrice.value.toStringAsFixed(2),
                      style: TextStyle(
                        color: XinHeColor('#E84128'),
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ]
              )),

              GestureDetector(
                onTap: (){

                  // Get.bottomSheet(XinHeInstantPayPage(),settings: RouteSettings(arguments: dataModel),persistent: false,isScrollControlled: true);

                  if(logic.specM.value.isSelected == false) {
                    XinHeToast.center('请选择商品数量');
                    return;
                  }

                  if(logic.type.value == 1){
                    logic.buyNow(logic.itemModel.value, logic.count.value);

                  }else {
                    logic.addToCart(logic.itemModel.value);
                  }
                  // Get.bottomSheet(XinHeInstantPayPage(dataModel: dataModel,totalPrice:logic.count.value == 1 ?dataModel.price!: state.totalPrice.value,count: logic.count.value,),settings: RouteSettings(arguments: dataModel),persistent: false,isScrollControlled: true);

                },
                child: Container(
                  height: 32,
                  margin: const EdgeInsets.only(left: 5,right: 15),
                  padding: const EdgeInsets.only(left: 15,right: 15),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(colors: [XinHeColor('#F89A30'),XinHeColor('#E84128')]),
                      borderRadius: BorderRadius.circular(16)
                  ),
                  child:  Text( logic.type.value == 1 ?'立即购买': '加入购物车',
                    style: const TextStyle(color: Colors.white,fontSize: 14,fontWeight: FontWeight.w500),
                    strutStyle: const StrutStyle(forceStrutHeight: true),
                  ),
                ),
              ),

            ],),
          ),
        )

      ],),
    ));
  }

  Widget _itemView(){
    final logic = Get.put(XinHeInstantBuyLogic());
    final state = Get.find<XinHeInstantBuyLogic>().state;
    return Stack(
      children: [
        Container(
          color: Colors.white,
          width: Get.width-30,
          height: 120,
          child:  Row(
            children: [



               Padding(
                padding:  const EdgeInsets.only(left: 0,top: 15,right: 15,bottom: 15),
                child: XinHeNetImage(url: logic.itemModel.value.iconPath??'',width: 90,height: 90) ,
              ),

              ///描述与价格数量
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                      width:Get.width-30-15-18-8-90-15,
                      child: Text(logic.itemModel.value.name??'',maxLines: 2,softWrap: true,style: TextStyle(color: XinHeColor('#222222'),fontSize: 14,fontWeight: FontWeight.bold),)
                  ),
                  const SizedBox(height: 20,),



                  SizedBox(
                    width: Get.width-30-15-18-8-90-15,
                    height: 23,
                    child: Row(
                      children: [

                        RichText(
                            text: TextSpan(children: [
                              ///￥
                              TextSpan(
                                text: '￥',
                                style: TextStyle(
                                  color: XinHeColor('#E84128'),
                                  fontSize: 9,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),

                              ///
                              TextSpan(
                                // text: '${dataModel.price??'0'}',
                                text: logic.specM.value.isSelected?'${logic.specM.value.price ??'0'}' : logic.getMaxMinPrice(logic.specList.value),
                                style: TextStyle(
                                  color: XinHeColor('#E84128'),
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),

                            ]
                            )),


                      ],
                    ),
                  )

                ],)


            ],
          ),
        ),

        Positioned(
          bottom: 0,
          right: 15,
          child: Container(
            width: Get.width-30-30,
            color: XinHeColor('#E7EEEE'),
            height: 0.5,),
        )

      ],
    );
  }

}
